/* bsp_config.c */

#include "bsp_config.h"
#include "fsl_device_registers.h"

#include "fsl_uart.h"


extern uint32_t SystemCoreClock;
uint32_t BSP_InitSystem(void)
{
    /* Switch on the clock for all the ports. */
    SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK
                | SIM_SCGC5_PORTB_MASK
                | SIM_SCGC5_PORTC_MASK
                | SIM_SCGC5_PORTD_MASK
                | SIM_SCGC5_PORTE_MASK;

    /* Update the Core clock record value. */
    SystemCoreClockUpdate();
    
    return SystemCoreClock;
}

void BSP_InitStdioUART(uint32_t baudrate)
{
    UART_Config_T MyUartConfigStruct = {
        .BusClkHz = BSP_CLK_CORECLK_HZ,
        .Baudrate = baudrate
    };
	/* FRDM-K64F */
    /* Clock Gate. */
    SIM->SCGC4 |= SIM_SCGC4_UART0_MASK;
    /* Pin mux. */
    PORTD->PCR[6] = PORT_PCR_MUX(3); /* UART0_RX. */
    PORTD->PCR[7] = PORT_PCR_MUX(3); /* UART0_TX. */
    
    UART_ConfigTransfer(UART0, &MyUartConfigStruct);
}

int fputc(int c, FILE *f)
{
    UART_PutTxDataBlocking(UART0, (uint8_t)c);
    return c;
}

int fgetc(FILE *f)
{
    return (UART_GetRxDataBlocking(UART0));
}

/* EOF. */

